<template>
  <div class="choice-commidy">
    <div class="already-content">
      <EditTable
        ref="editTable"
        @onChangeSelectAll="onSelectAll"
        @onJoinSale="onJoinSale"
        @onJoinSaleCheck="onJoinSaleCheck"
        @onShowChild="onShowChild"
        :status="status"
        :listData="columns1"
        :list="$store.state.giftSale.giftData "
      ></EditTable>
    </div>
  </div>
</template>
<script>
import EditTable from "@/views/sales/common/TableChoiceSale"
import { columns1 } from "./ChoicePrevent.js"
import {compareSaleGift, getCode, setArrayValue, setAdd, setExchangeValue, onJoinSaleBody} from "@/views/sales/common/TableSaleMethods"
import { getValueArray, setEmptyArray, getSelectStatus, getValueStatus, setExchange } from "./ChoiceCommidy.js"
export default {
  name: "choicecommidy",
  components: {
    EditTable
  },
  mounted() {
    if( !this.$store.state.giftSale.giftData.length ) {
      this.getProductData({size: 5000})
    } 
  },
  data() {
    return {
      model1: "",
      value: "",
      cityList: [],
      columns1,
      data1: [],
      status: "loading"
    };
  },
  methods: {
    onShowChild(item) {
      item.showChildren = !item.showChildren
    },
    getProductData(param) {
      let self = this;
      self.status = "loading"
      this.$ajaxGet("/activity/product/spu_sku_page", param).then(
        data => {
          self.$store.state.giftSale.giftData = self.data1 = data.records;
          setAdd(self, self.data1, {
            index: "checked",
            val1: false,
            childName: "productSkuList"
          });
          setAdd(self, self.data1, {
            index: "showChildren",
            val1: false,
            childName: "productSkuList"
          });
          setAdd(self, self.data1, {
            index: "buttonChecked",
            val1: false,
            childName: "productSkuList"
          });
          setAdd(self, self.data1, {
            index: "returnPrice",
            val1: "",
            childName: "productSkuList"
          });
          setAdd(self, self.data1, {
            index: "quantity",
            val1: "1",
            childName: "productSkuList"
          });
          setExchange(self, self.data1, {
            index: "returnPrice",
            index1: "salePrice",
            childName: "productSkuList"
          });
          getCode (self, self.data1, {
              index: 'parentcode',
              childName: "productSkuList"
          })
          self.status = "pass";
          compareSaleGift(self.$store.state.giftSale.giftData, self.$store.state.giftSale.GetgiftData)
        }
      );
    },
    getSaleData(type) {
      //
      this.$store.commit("getgiftGiftData", type);
    },
    onSelectAll(param) {
      //点击选中所有checked按钮的时候出发的事件
      param ? setArrayValue(this.$store.state.giftSale.giftData, {
        index: "checked",
        val1: true,
        childName: "productSkuList"
      }) : setArrayValue(this.$store.state.giftSale.giftData, {
        index: "checked",
        val1: false,
        childName: "productSkuList"
      })
    },
    onJoinSaleBody(item, todo, params) {
      onJoinSaleBody(item, todo, params)
    },
    onJoinSale(item, todo, selectAll) {
      //点击加入促销的时候触发的事件
      item.buttonChecked = !item.buttonChecked;
      this.onJoinSaleBody(item, todo, {index:'checked',index1: 'productSkuList' })
      this.onJoinSaleBody(item, todo, {index:'buttonChecked',index1: 'productSkuList' })
      this.$store.commit("deleteGiftGiftData", {todo: todo, item: item})
      this.getSaleData('buttonChecked')
    },
    onJoinSaleCheck(item, todo, selectAll) {
      // 点击checked按钮的时候触发的事件
      this.onJoinSaleBody(item, todo, {index:'checked',index1: 'productSkuList' })
    }
  }
};
</script>

